<template>
  <div id="updDormitoryMasterDialog">
    <el-dialog title="宿管" :visible.sync="updDormitoryMasterDialogVisible" width="300px" :before-close="close">
      <el-form :model="dormitoryMasterForm" ref="dormitoryMasterForm" label-width="80px" label-position="left"
               :rules="rules">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="dormitoryMasterForm.name" placeholder="请输入楼栋名称" style="width: 180px"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="dormitoryMasterForm.gender" placeholder="请选择性别" style="width: 180px">
            <el-option
              v-for="item in genderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="dormitoryMasterForm.phone" placeholder="请输入电话号码" maxlength="11"
                    style="width: 180px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="updDormitoryMaster">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import dormitoryMaster from "../../api/dormitory/dormitoryMaster";

  export default {
    name: "updDormitoryMasterDialog",
    props: ['updDormitoryMasterDialogVisible', 'dormitoryMasterForm'],
    data() {
      /*电话号码校验器*/
      var checkPhone = (rule, value, callback) => {
        const phoneReg = /^1[34578]\d{9}$/
        if (!value) {
          return callback(new Error('电话号码不能为空'))
        }
        setTimeout(() => {
          // Number.isInteger是es6验证数字是否为整数的方法,实际输入的数字总是识别成字符串
          // 所以在前面加了一个+实现隐式转换

          if (!Number.isInteger(+value)) {
            callback(new Error('请输入数字值'))
          } else {
            if (phoneReg.test(value)) {
              callback()
            } else {
              callback(new Error('电话号码格式不正确'))
            }
          }
        }, 100)
      }
      return {
        genderOptions: [
          {
            label: '男',
            value: 0
          },
          {
            label: '女',
            value: 1
          }
        ],
        rules: {
          name: [
            {required: true, message: '请输入寝室名', trigger: 'blur'},
            {max: 10, message: '长度小于10字符', trigger: 'blur'}
          ],
          gender: [
            {required: true, message: '请选择性别', trigger: 'change'}
          ],
          phone: [
            {required: true, message: '请输入电话号码', trigger: 'change'},
            {validator: checkPhone, trigger: 'blur'}
          ]
        }
      }
    },
    created() {
    },
    methods: {

      /*添加宿管*/
      updDormitoryMaster() {
        this.$refs["dormitoryMasterForm"].validate(valid => {
          if (valid) {
            dormitoryMaster.updDormitoryMaster(this.dormitoryMasterForm).then(() => {
              this.$message.success("修改成功")
              this.close()
              this.$parent.getDormitoryMaster()
            })
          }else {
            this.$message.error("请正确填写信息")
          }
        })
      },
      /*关闭dialog*/
      close() {
        this.$refs['dormitoryMasterForm'].resetFields()
        this.$parent.closeDialog()
      }
    }
  }
</script>

<style scoped>

</style>
